package com.wd.live.view.fragment;

/**
 * <p>项目名称:LookFor2.0</p>
 * <p>包名:com.bawei.lookfor20.view.fragment</p>
 * <p>简述:</p>
 *
 * @author 王柯浩
 * @date 2022/11/9
 */
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wd.live.view.MusicActivity;
import com.wd.live.R;
import com.wd.live.base.BaseFragment;


import com.wd.live.bean.LiveTypeListBean;
import com.wd.live.databinding.BroadcastFragmentBinding;
import com.wd.live.viewmodel.LiveViewModel;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

public class BroadcastFragment extends BaseFragment<LiveViewModel, BroadcastFragmentBinding> {
    boolean a=false;
    private File file;
    @Override
    protected void initData() {
        databind.livefm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //调用相册
                Intent intent1 = new Intent();
                intent1.setAction(Intent.ACTION_PICK);
                intent1.setType("image/*");
                startActivityForResult(intent1,100);
            }
        });
        databind.zbmusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MusicActivity.class));
            }
        });
        databind.livetype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(a){
                    databind.livetyperec.setVisibility(View.GONE);
                }else{
                    databind.livetyperec.setVisibility(View.VISIBLE);
                    viewmodel.getLiveType().observe(getActivity(), new Observer<LiveTypeListBean>() {
                        @Override
                        public void onChanged(LiveTypeListBean liveTypeBeen) {
                            List<LiveTypeListBean.ResultBean> result = liveTypeBeen.getResult();
                            Log.v("list",result.toString());
                            databind.livetyperec.setLayoutManager(new GridLayoutManager(getActivity(),3));
                            databind.livetyperec.setAdapter(new CommonAdapter<LiveTypeListBean.ResultBean>(getActivity(),R.layout.livetype_recitem,result) {
                                @Override
                                protected void convert(ViewHolder holder,LiveTypeListBean.ResultBean resultBean, int position) {
                                    holder.setText(R.id.signlive,resultBean.getName());
                                    TextView viewById = holder.itemView.findViewById(R.id.signlive);
                                    viewById.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            viewById.setBackgroundResource(R.drawable.livetype_kuangseclet);
                                            viewById.setTextColor(R.color.live_text);

                                        }
                                    });

                                }
                            });
                        }
                    });
                }
                a=!a;
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100){
            ContentResolver contentResolver = getActivity().getContentResolver();
            try {
                InputStream inputStream = contentResolver.openInputStream(data.getData());
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                //创建文件夹
                file = new File("data/data/com.wd.live/image.png");
                if(!file.exists()){
                    file.createNewFile();
                }
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG,50,bufferedOutputStream);
                //关闭流
                bufferedOutputStream.flush();
                bufferedOutputStream.close();
                databind.livefm.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    protected int initLayout() {
        return R.layout.broadcast_fragment;
    }

    @Override
    public void onChanged(Object o) {

    }
}